package com.test.mytest.javacv;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Canvas;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.payment.WeixinPayProxy;
import com.foxinmy.weixin4j.payment.mch.MchPayRequest;
import com.foxinmy.weixin4j.payment.mch.Order;
import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.type.IdType;
import com.google.zxing.NotFoundException;
import com.test.mytest.javacv.JavacvService.BeforePainListener;
import com.test.util.QRCodeUtil;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.UUID;
import java.awt.event.ActionEvent;

public class QrDecoderTestApp {

	private JFrame frame;
	private JTextField textField;
	private JLabel lbl_msg;
	
	JavacvService javacvService = new JavacvService();
	private Canvas canvas;
	int size=0;
	protected static final String id = "wxeb3574cc1d31403d";
	protected static final String paySignKey = "4edc75320d1043d2bec5186132279e2a";
	protected static final String mchId = "1495367402";
	protected static final String secret = null;
	WeixinPayProxy payProxy = new WeixinPayProxy(new WeixinPayAccount(id, paySignKey, mchId));
	boolean paying=false;
	
	
	private String outTradeNo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					QrDecoderTestApp window = new QrDecoderTestApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public QrDecoderTestApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel label = new JLabel("扫描");
		panel.add(label);
		
		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(30);
		
		JButton button = new JButton("开始");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				javacvService.play(canvas);
			}
		});
		panel.add(button);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		canvas = new Canvas();
		panel_1.add(canvas, BorderLayout.CENTER);
		
		JPanel panel_2 = new JPanel();
		frame.getContentPane().add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		lbl_msg = new JLabel("");
		panel_2.add(lbl_msg);
		javacvService.start();
		javacvService.setFace(false);
		javacvService.addBeforePainListener(new BeforePainListener() {
			@Override
			public void beforePain(BufferedImage image) {
				if(paying){
					return;
				}
				long currentTimeMillis = System.currentTimeMillis();
				try {
					
					String decode = QRCodeUtil.decode(image);
					textField.setText(size+++"=="+decode);
					
				} catch (NotFoundException e) {
					
				}finally{
					System.out.println(System.currentTimeMillis()-currentTimeMillis);
				}
			}
		});
	}
	
	
	public void pay(String authCode){
		try {
			outTradeNo = UUID.randomUUID().toString().replace("-", "");
			MchPayRequest mchPayRequest = payProxy.createMicroPayRequest(authCode, "测试收费", outTradeNo, 1, "192.168.2.20", "测试");
			System.out.println(mchPayRequest.toRequestString());
		} catch (WeixinException e) {
			e.printStackTrace();
			lbl_msg.setText(e.getMessage());
		}
	}
	/**
	 * 
	 */
	public void queryPayResult() {
		try {
			Order order = payProxy.queryOrder(new IdQuery(outTradeNo, IdType.TRADENO));
			System.out.println(outTradeNo+":"+order.getTradeState()+":"+order.getTradeStateDesc());
		} catch (WeixinException e1) {
			e1.printStackTrace();
		}
	}

}
